"use strict";
/* Controllers */
app.controller("smartTestItemController", ['$scope', '$location', '$rootScope', '$routeParams', 'smartTestItemService', function ($scope, $location, $rootScope, $routeParams, smartTestItemService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.type = $routeParams.type;
    if(vm.p.type=='id') {
        vm.p.tableId = $routeParams.id;
    }else {
        vm.p.tableName = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.getLogicTable = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 请求参数
        var params = {
            tableId: vm.p.tableId,
            tableName: vm.p.tableName
        };

        // 结果处理
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null || typeof(data)=='undefined') {
                return;
            }
            vm.result.data = data;
            vm.p.id = data.id; // topicTbl需要设置

            var columnListFmt = function (val) {
                if(typeof(val)=='undefined') {
                    return null;
                }
                var ret = [];
                var list = val.split(',');
                for (var i in list) {
                    var it = list[i].split('^');
                    ret.push({'columnName':it[0],'columnNameCn':it[1],'columnType':it[2],'columnComment':it[3]});
                }
                return ret;
            };

            var columnList = columnListFmt(data.columnList);
            if(columnList!=null) {
                $('#list_table').bootstrapTable('load', columnList);
            }
        };

        smartTestItemService.getLogicTable(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageItem: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.getLogicTable();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('smartTestItemService', ['ajaxService', function (ajaxService) {

    this.getLogicTable = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/smartdq/getLogicTable.do", successFunction, errorFunction);
    };
}]);


function columnNameFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return value + '('+row.columnNameCn+')';
}

function topicTblDescFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    if(typeof(row.columnComment)=='undefined'
        || row.columnComment==null || row.columnComment=='-') {
        return value;
    }
    return value + '，'+row.columnComment;
}


